from utils.SendMethod import SendMethod
from utils.GetKeyword import GetKeyword

class MemberInterface:
    def __init__(self,url):
        self.url = url

    def get_auth_code(self,telephone):
        method = 'get'
        url = self.url +'sso/getAuthCode'
        payload ={'telephone':telephone}
        return SendMethod.send_method(method=method,url=url,params=payload)

    def get_verify_code(self,telephone):
        response = self.get_auth_code(telephone)
        return GetKeyword.get_keyword(response,'data')

    def register(self,body):
        method = 'post'
        url = self.url + 'sso/register'

        return SendMethod.send_method(method=method,url=url,data=body)

    def login(self,body):
        method = 'post'
        url = self.url +'sso/login'
        return SendMethod.send_method(method=method,url=url,data=body)

    def get_member_token(self,body):
        response = self.login(body)
        token = GetKeyword.get_keyword(response,'token')
        return {'Authorization': f'Bearer {token}'}

    def get_user_info(self,name):
        method = 'get'
        url = self.url +'sso/info'
        payload ={'name':name}
        return SendMethod.send_method(method=method,url=url,params=payload,headers=self.get_member_token(body))

    def update_pwd(self,body):
        method = 'post'
        url = self.url + 'sso/updatePassword'
        return SendMethod.send_method(method=method, url=url, data=body)



if __name__ =='__main__':
    url = "http://47.108.206.100:8085/"
    member = MemberInterface(url)
    telephone = '17612341234'
    print(member.get_auth_code('17612341234'))
    print(member.get_verify_code('17612341234'))

#     body = {
#     "authCode":member.get_verify_code(telephone),
#     "username":"jack",
#     "password":"123456",
#     "telephone" :"18912341234"
# }
#
#     print(member.register(body))
    body = {"username":"jack",
            "password":"123456"
           }
    print(member.login(body))
    # print(member.get_member_token(body))

    # print(member.get_user_info('jack'))
    # body = {
    #     "authCode":member.get_verify_code(telephone),
    #     "password":"123456",
    #     "telephone":"17612341234"
    # }
    # print(member.update_pwd(body))



